package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 一百三十三、癌症患者身心状态量表
 */
public class Table133 extends AbstractTable implements TableCalculate {

    String code = "Cancer";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return this.GetCancerFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return this.GetCancerReport(map, (int) map.get("gender"));
    }

    private ClsAnswerResult GetCancerFactorScore(ClsAnswerRequest answer) {
        // 心理、躯体、社会功能、心理行为的可塑性、总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 16;
            // 后面计算评价的时候用于判断用那种常模;0代表总体常模，1代表性别常模
            int intMode = 0;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 共2组，1,2,3,4,5和5,4,3,2,1
            // 1心理、2躯体、3社会功能、4心理行为的可塑性、5总分
            String[] strNumberA = {"1,3,6,9,12", "2,8,10,13", "4,7,14,16", "5,11,15"};
            String[] strSubA;

            // 指导意见，1-5因子
            String[] vDoctorIdeaA = new String[5];

            float[][] vFactorA = new float[5][4]; // 实际为5个因子,原始分、总体人群标准分、性别人群标准分，最终使用的标准分
            for (i = 0; i < vFactorA.length; i++) {
                for (j = 0; j < vFactorA[1].length; j++) {
                    vFactorA[i][j] = 0;
                }
            }

            // 前4个因子原始分
            for (i = 0; i < strNumberA.length; i++) {
                strSubA = strNumberA[i].split(",");
                if (i == strNumberA.length - 1)// 最后一个因子反向计分
                {
                    for (j = 0; j < strSubA.length; j++) {
                        vFactorA[i][0] += 6 - Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]);
                    }
                } else {
                    for (j = 0; j < strSubA.length; j++) {
                        vFactorA[i][0] += Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]);
                    }
                }
            }
            // 总分原始分
            vFactorA[4][0] += vFactorA[0][0] + vFactorA[1][0] + vFactorA[2][0] + vFactorA[3][0];

            // 1男、0女
            int intSex = answer.getSex();

            // 总体人群常模均值
            float[] norm_m = {12.32f, 12.55f, 13.03f, 13.1f, 51f};
            // 总体人群常模标准差
            float[] norm_sd = {3.53f, 2.52f, 4.52f, 3.17f, 8.42f};

            // 性别常模均值
            float[] norm_sex_m;
            // 性别常模标准差
            float[] norm_sex_sd;

            switch (intSex) {
                case 1: // 男
                    norm_sex_m = new float[]{12.02f, 12.74f, 12.59f, 13.48f, 50.83f};
                    norm_sex_sd = new float[]{4.26f, 3.19f, 4.23f, 3.01f, 9.13f};
                    break;
                case 0: // 女
                    norm_sex_m = new float[]{12.53f, 13.04f, 12.76f, 13.36f, 52.58f};
                    norm_sex_sd = new float[]{4.31f, 3.12f, 3.99f, 3f, 9.04f};
                    break;
                default: // 理论上不会，实在没有性别就当男人处理
                    norm_sex_m = new float[]{12.02f, 12.74f, 12.59f, 13.48f, 50.83f};
                    norm_sex_sd = new float[]{4.26f, 3.19f, 4.23f, 3.01f, 9.13f};
                    break;
            }

            for (i = 0; i < 5; i++) {
                vFactorA[i][1] = (vFactorA[i][0] - norm_m[i]) / norm_sd[i];
                BigDecimal bd = new BigDecimal((vFactorA[i][0] - norm_sex_m[i]) / norm_sex_sd[i]);
                vFactorA[i][2] = bd.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            }

            // 使用两个常模的总分分别计算Z值并取绝对值，比较哪个值大，就使用这个值对应的常模计算其他因子，并得出评价
            if (Math.abs(vFactorA[4][1]) - Math.abs(vFactorA[4][2]) >= 0) {
                intMode = 0;
                for (i = 0; i < 5; i++) {
                    vFactorA[i][3] = vFactorA[i][1];
                }
            } else {
                intMode = 1;
                for (i = 0; i < 5; i++) {
                    vFactorA[i][3] = vFactorA[i][2];
                }
            }

            float[][] sngValueA;
            switch (intMode) {
                case 0:
                    sngValueA = new float[][]{{15.291f, 18.126f, 20.532f, 23.228f},
                            {14.671f, 16.695f, 18.412f, 20.337f}, {16.834f, 20.465f, 23.545f, 26.998f},
                            {15f, 18.314f, 20.474f, 22.896f}, {58.086f, 64.850f, 70.587f, 77.019f}};
                    break;
                case 1:
                    switch (intSex) {
                        case 1: // 男
                            /*
                             * sngValueA = new float[][] {{15.605f,19.027f,21.930f,25.184f},
                             * {15.425f,17.987f,20.161f,22.598f},{16.150f,19.548f,22.430f,25.662f},
                             * {16.013f,18.431f,20.482f,22.782f},{58.514f,65.848f,72.069f,79.044f}};
                             */
                            sngValueA = new float[][]{{15.605f, 19.027f, 21.930f, 25.184f},
                                    {15.425f, 17.987f, 20.161f, 22.598f}, {16.150f, 19.548f, 22.430f, 25.662f},
                                    {15f, 18.431f, 20.482f, 22.782f}, {58.514f, 65.848f, 72.069f, 79.044f}};
                            break;
                        case 0: // 女
                            /*
                             * sngValueA = new float[][] {{16.157f,19.620f,22.556f,25.849f},
                             * {15.666f,18.172f,20.298f,22.681f},{16.118f,19.323f,22.042f,25.090f},
                             * {15.885f,18.295f,20.339f,22.631f},{60.188f,67.450f,73.610f,80.515f}};
                             */
                            sngValueA = new float[][]{{16.157f, 19.620f, 22.556f, 25.849f},
                                    {15.666f, 18.172f, 20.298f, 22.681f}, {16.118f, 19.323f, 22.042f, 25.090f},
                                    {15f, 18.295f, 20.339f, 22.631f}, {60.188f, 67.450f, 73.610f, 80.515f}};
                            break;
                        default: // 理论上不会，实在没有性别就当男人处理
                            /*
                             * sngValueA = new float[][] {{15.605f,19.027f,21.930f,25.184f},
                             * {15.425f,17.987f,20.161f,22.598f},{16.150f,19.548f,22.430f,25.662f},
                             * {16.013f,18.431f,20.482f,22.782f},{58.514f,65.848f,72.069f,79.044f}};
                             */
                            sngValueA = new float[][]{{15.605f, 19.027f, 21.930f, 25.184f},
                                    {15.425f, 17.987f, 20.161f, 22.598f}, {16.150f, 19.548f, 22.430f, 25.662f},
                                    {15f, 18.431f, 20.482f, 22.782f}, {58.514f, 65.848f, 72.069f, 79.044f}};
                            break;
                    }
                    break;
                default:
                    sngValueA = new float[][]{{15.291f, 18.126f, 20.532f, 23.228f},
                            {14.671f, 16.695f, 18.412f, 20.337f}, {16.834f, 20.465f, 23.545f, 26.998f},
                            {15f, 18.314f, 20.474f, 22.896f}, {58.086f, 64.850f, 70.587f, 77.019f}};
                    break;
            }

            for (i = 0; i < 5; i++) {
                vDoctorIdeaA[i] = "Cancer." + String.valueOf(i + 1) + ".5";
                for (j = 0; j < sngValueA[i].length; j++) {
                    if (vFactorA[i][0] < sngValueA[i][j]) {
                        vDoctorIdeaA[i] = "Cancer." + String.valueOf(i + 1) + "." + String.valueOf(j + 1);
                        break;
                    }
                }
            }

            ClsScore.Cancer cancer = new ClsScore.Cancer();

            cancer.setMode(intMode);

            cancer.setXl_o(vFactorA[0][0]);
            cancer.setXl_z_s(vFactorA[0][1]);
            cancer.setXl_sex_s(vFactorA[0][2]);
            cancer.setXl_s(vFactorA[0][3]);
            cancer.setXl_d(vDoctorIdeaA[0]);

            cancer.setQt_o(vFactorA[1][0]);
            cancer.setQt_z_s(vFactorA[1][1]);
            cancer.setQt_sex_s(vFactorA[1][2]);
            cancer.setQt_s(vFactorA[1][3]);
            cancer.setQt_d(vDoctorIdeaA[1]);

            cancer.setShgn_o(vFactorA[2][0]);
            cancer.setShgn_z_s(vFactorA[2][1]);
            cancer.setShgn_sex_s(vFactorA[2][2]);
            cancer.setShgn_s(vFactorA[2][3]);
            cancer.setShgn_d(vDoctorIdeaA[2]);

            cancer.setXlxw_o(vFactorA[3][0]);
            cancer.setXlxw_z_s(vFactorA[3][1]);
            cancer.setXlxw_sex_s(vFactorA[3][2]);
            cancer.setXlxw_s(vFactorA[3][3]);
            cancer.setXlxw_d(vDoctorIdeaA[3]);

            cancer.setTotal_o(vFactorA[4][0]);
            cancer.setTotal_z_s(vFactorA[4][1]);
            cancer.setTotal_sex_s(vFactorA[4][2]);
            cancer.setTotal_s(vFactorA[4][3]);
            cancer.setTotal_d(vDoctorIdeaA[4]);

            result.setObject(cancer);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }
    
 // 一百三十三、癌症患者身心状态量表
 	// 这个量表放在抗癌卫士里面，此处报告仅供内部测试参考使用
 	private String[] GetCancerReport(Map map, int vSex) {
 		// 下面因子名称字段改成.toLowerCase()小写，是因为JAVA转json的时候会把大写字母转成小写字母
 		// 第一个，错误信息；第二个，量表得分部分；第三个，量表指导意见_d数组部分
 		// String[] strResult = {"","",""};
 		String[] strResult = { "", "", "", "", "" };
 		try {
 			int i = 0;

 			String[] gcName2 = "心理,躯体,社会功能,心理行为的可塑性,心身状态".split(",");
 			String[] geName2 = "xl,qt,shgn,xlxw,total".split(",");
 			String[] gsName2 = "12.32±3.53,12.55±2.52,13.03±4.52,13.10±3.17,51.00±8.42".split(",");

 			if (Integer.valueOf(String.valueOf(map.get("mode") == null ? "0" : map.get("mode"))) == 0) {
 				gsName2 = "12.32±3.53,12.55±2.52,13.03±4.52,13.10±3.17,51.00±8.42".split(",");
 			} else {
 				switch (vSex) {
 				case 1:// 男
 					gsName2 = "12.02±4.26,12.74±3.19,12.59±4.23,13.48±3.01,50.83±9.13".split(",");
 					break;
 				case 0:// 女
 					gsName2 = "12.53±4.31,13.04±3.12,12.76±3.99,13.36±3.00,52.58±9.04".split(",");
 					break;
 				}
 			}

 			String[] gvName2 = {};
 			gvName2 = gcName2;

 			if (g_inblFactor) // 因子得分
 			{
 				strResult[1] += "<table cellpadding='0' cellspacing='0' width='100%' border='0'>";

 				strResult[1] += "<tr><td" + subTitle(0, oWidth * 3) + "></td><td" + subTitle(0, tWidth) + ">原始分</td><td"
 						+ subTitle(0, tWidth) + ">标准分</td>" + "<td" + subTitle(0, -1) + ">均分±标准差</td></tr>";

 				for (i = 0; i < gcName2.length; i++) {
 					strResult[1] += "<tr><td" + subTitle(0, -1) + ">" + gvName2[i] + "</td><td" + subValue(0, -1) + ">"
 							+ map.get(geName2[i] + "_o") + "</td>" + "<td" + subValue(0, -1) + ">"
 							+ map.get(geName2[i] + "_s") + "</td><td" + subValue(0, -1) + ">" + gsName2[i]
 							+ "</td></tr>";
 				}

 				strResult[1] += "</table>";
 			}

 			String strCode = "";
 			for (i = 0; i < geName2.length - 1; i++) {
 				strCode += map.get(geName2[i] + "_d") + "|";
 			}
 			if (strCode.length() > 0) {
 				strCode = strCode.substring(0, strCode.length() - 1);
 			}

 			strResult[2] = strCode;

 			// 按照量表总均分预警
 			String strTemp = "";

 			strTemp = "total_d";

 			switch (String.valueOf(map.get(strTemp)).split("\\.")[2]) {
 			case "1":
 				strResult[3] = "0";
 				strResult[4] = "正常";
 				break;
 			case "2":
 				strResult[3] = "0";
 				strResult[4] = "略高";
 			case "3":
 				strResult[3] = "0";
 				strResult[4] = "较高";
 			case "4":
 				strResult[3] = "*";
 				strResult[4] = "异常";
 			case "5":
 				strResult[3] = "*";
 				strResult[4] = "极端异常";
 				break;
 			}

 			return strResult;
 		} catch (ArithmeticException e) {
 			strResult[0] = "失败";
 			return strResult;
 		}
 	}


}
